package com.wn.demo04;

import com.wn.demo03.Person;

import javax.swing.text.html.Option;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.BinaryOperator;

/**
 * @author yellow Docter
 * @date 2024 -06-17
 * @desc
 *   归约的操作:
 *       {1,2,3} ===> 加法归约   6
 *       {1,2,3} ===> 乘法归约   6
 */
public class Demo02 {

    static List<Person> persons = Arrays.asList(
            new Person("马云", 48, 2000d),
            new Person("马化腾", 50, 3000d),
            new Person("雷军", 46, 4000d),
            new Person("刘强东", 47, 5000d),
            new Person("张朝阳", 50, 6000d),
            new Person("张朝阳", 50, 6000d),
            new Person("张一鸣", 50, 7000d),
            new Person("张一鸣", 50, 7000d)
    );

    public static void main(String[] args) {

        BinaryOperator t;

       Optional<Double> option =  persons.stream()
                .filter(e->e.getSalary() > 5000)
                .distinct()
                .map(x->x.getSalary())  //把 Person 转成了 double
                .reduce((x,y)->x+y);   // //归约 double;
        Double totalSal = option.get();
        System.out.println("totalSal = " + totalSal);


        System.out.println("*********************");
        List<Integer> list = Arrays.asList(1, 2, 3);

        //规约的说话 有一个起始条件 0
        Integer reduce = list.stream().reduce(0, (x, y) -> x + y);
        System.out.println("reduce = " + reduce);
        System.out.println("*********************");

        //规约的说话 有一个起始条件 10
        Integer reduce2 = list.stream().reduce(10, (x, y) -> x + y);
        System.out.println("reduce2 = " + reduce2);

    }

}
